package com.cqtczh.qhd.driver.model;

import java.io.File;

import tools.SD_Finder;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.cqtczh.qhd.driver.R;
import com.cqtczh.qhd.driver.api.GagApi;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

/**
 * * @author 作者 陈修园:
 * 
 * @date 创建时间：2015-5-13 下午1:27:59
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class DownLoadAPP extends Service {
	private int BuildId = 10111;
	private NotificationManager notificationManager;
	private Notification notification;
	private String tag = "Q货的下载中...";
	private String root;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		Log.v("DownLoadAPP--service", "onBind");
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		Log.v("service", "onCreate");
		super.onCreate();

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.v("service", "onStartCommand");
		if (intent==null) {
			stopSelf();
			
		}
		root = intent.getStringExtra("path");
		if (root == null || root.length() < 0) {

		} else {
			StrartDownLoadAPK();
		}
		return super.onStartCommand(intent, flags, startId);

	}

	@SuppressWarnings("deprecation")
	private void StrartDownLoadAPK() {

		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notification = new Notification(R.drawable.ic_launcher, "Q货的",
				System.currentTimeMillis());
		notification.contentView = new RemoteViews(getPackageName(),
				R.layout.down_load_app);
		notification.contentView.setProgressBar(R.id.pb, 100, 0, false);
		notificationManager.notify(BuildId, notification);

		Log.v("StrartDownLoadAPK", "StrartDownLoadAPK");
		HttpUtils httpUtils = new HttpUtils();

		String file_path_and_name = root + GagApi.FILE_PATH + "sj.apk";
		httpUtils.download(GagApi.APK_DowndLoad_Path, file_path_and_name,
				false, true, new RequestCallBack<File>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						Log.v("onFailure", arg1);
						// Log.v("onFailure", arg0.getLocalizedMessage());
						// Log.v("onFailure", arg0.);
						// Log.v("onFailure", arg0.getLocalizedMessage());
						notificationManager.cancel(BuildId);
					}

					@Override
					public void onLoading(long total, long current,
							boolean isUploading) {
						// TODO Auto-generated method stub
						super.onLoading(total, current, isUploading);
						Log.v("是否上传", "" + isUploading);
						Log.v("数据总大小", "" + total);
						Log.v("已下载大小", "" + current);
						
						if (current > 0) {

							Long cut = current % total;
							Log.v("百分比", cut+ "");
							// int get=0;
							cut = cut / 10;
							for (int i = 0; (cut / 10) >= 100; i++) {
								cut = cut / 10;
							}
							// Log.v("除法", cut+"%");
							notification.contentView.setTextViewText(
									R.id.down_tv, tag + (int) (cut / 10) + "%");
							notification.contentView.setProgressBar(R.id.pb,
									100, (int) (cut / 10), false);
							notificationManager.notify(BuildId, notification);
						}
					}

					@Override
					public void onSuccess(ResponseInfo<File> responseInfo) {
						// TODO Auto-generated method stub
						Log.v("下载成功！", responseInfo.result.getPath());
						Intent i = new Intent(Intent.ACTION_VIEW);
						i.setDataAndType(
								Uri.parse("file://"
										+ responseInfo.result.getPath()),
								"application/vnd.android.package-archive");
						i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						DownLoadAPP.this.startActivity(i);
						// stopSelf();
						notificationManager.cancel(BuildId);
						DownLoadAPP.this.stopSelf();
					}
				});
		// Log.v("stopSelf", "stopSelf");

	}

}
